Imports System.Windows.Forms
Imports Vintasoft.Imaging.Codecs
Imports Vintasoft.Imaging

Namespace Imaging.Codecs.Dialogs
	Public Partial Class JpegEncoderSettingsForm
		Inherits Form
		Public Sub New()
			InitializeComponent()
			EditAnnotationSettings = False
		End Sub


		#Region "Properties"

		Private _encoderSettings As JpegEncoderSettings
		''' <summary>
		''' Gets or sets JPEG encoder settings.
		''' </summary>
		Public Property EncoderSettings() As JpegEncoderSettings
			Get
				Return _encoderSettings
			End Get
			Set
				If value Is Nothing Then
					Throw New ArgumentOutOfRangeException()
				End If
				If _encoderSettings IsNot value Then
					_encoderSettings = value
					ShowEncoderSettings()
				End If
			End Set
		End Property


		Public Property EditAnnotationSettings() As Boolean
			Get
				Return tabControl1.TabPages.Contains(annotationsTabPage)
			End Get
			Set
				If EditAnnotationSettings <> value Then
					If value Then
						tabControl1.TabPages.Add(annotationsTabPage)
					Else
						tabControl1.TabPages.Remove(annotationsTabPage)
					End If
				End If
			End Set
		End Property

		Private _formatOfAnnotations As AnnotationsFormat
		Public Property FormatOfAnnotations() As AnnotationsFormat
			Get
				Return _formatOfAnnotations
			End Get
			Set
				_formatOfAnnotations = value
				annotationsBinaryCheckBox.Checked = (_formatOfAnnotations And AnnotationsFormat.VintasoftBinary) <> 0
				annotationXmpCheckBox.Checked = (_formatOfAnnotations And AnnotationsFormat.VintasoftXmp) <> 0
			End Set
		End Property


		#End Region



		#Region "Methods"

		Protected Overrides Sub OnLoad(e As EventArgs)
			MyBase.OnLoad(e)

			If EncoderSettings Is Nothing Then
				EncoderSettings = New JpegEncoderSettings()
			End If
		End Sub

		Private Sub ShowEncoderSettings()
			jpegGrayscaleCheckBox.Checked = EncoderSettings.SaveAsGrayscale
			jpegQualityNumericUpDown.Value = EncoderSettings.Quality
			disableSubsamplingCheckBox.Checked = EncoderSettings.IsSubsamplingDisabled
		End Sub

		Private Sub SetEncoderSettings()
			EncoderSettings.SaveAsGrayscale = jpegGrayscaleCheckBox.Checked
			EncoderSettings.Quality = CInt(Math.Truncate(jpegQualityNumericUpDown.Value))
			EncoderSettings.IsSubsamplingDisabled = disableSubsamplingCheckBox.Checked

			If EditAnnotationSettings Then
				_formatOfAnnotations = AnnotationsFormat.None
				If annotationsBinaryCheckBox.Checked Then
					_formatOfAnnotations = _formatOfAnnotations Or AnnotationsFormat.VintasoftBinary
				End If
				If annotationXmpCheckBox.Checked Then
					_formatOfAnnotations = _formatOfAnnotations Or AnnotationsFormat.VintasoftXmp
				End If
			End If
		End Sub

		Private Sub buttonCancel_Click(sender As Object, e As EventArgs)
			DialogResult = DialogResult.Cancel
		End Sub

		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			SetEncoderSettings()

			DialogResult = DialogResult.OK
		End Sub


		#End Region



	End Class
End Namespace
